package services

import (
	"encoding/json"
	"gorm.io/gorm"
	"mygo/app/models"
	"mygo/app/tools"
	"strings"
)

func HandelWzhy(forecast *models.Forecast, time string, db *gorm.DB) {
	timeArr := strings.Split(time, "-")
	year := timeArr[0]
	month := timeArr[1]
	day := timeArr[2]

	timeNow := year + "-" + month + "-" + day
	y, m, d := tools.GetNextDay(year, month, day, 1)
	timeNow = y + "-" + m + "-" + d
	var foreList models.ForeList
	db.Where("code = ?", "rahx").First(&foreList)
	var foreDetail models.ForeDetailJson
	db.Where("foreListId = ? AND time = ? AND state != ?", foreList.Id, time, "0").First(&foreDetail)
	var raData = models.Forecast{}
	json.Unmarshal(foreDetail.Data, &raData)

	for i, report := range forecast.Reports {
		if i == 0 {
			stations := []string{"温州", "瑞安", "鳌江"}
			for j := 1; j < 4; j++ {
				var tide models.TwcTide
				db.Where("date = ? AND station = ?", timeNow, stations[j-1]).First(&tide)
				//report.Data[j][1].Value = tide.TimeHigh1
				//report.Data[j][2].Value = tide.TimeHigh2
				//report.Data[j][3].Value = tide.TimeLow1
				//report.Data[j][4].Value = tide.TimeLow2
				for idx, key := range []string{tide.TimeHigh1, tide.TimeHigh2, tide.TimeLow1, tide.TimeLow2} {
					tArr := strings.Split(key, ":")
					if len(tArr) == 2 {
						report.Data[j][idx+1].Value = tArr[0] + "点" + tArr[1] + "分"
					}
				}
			}
		} else if i == 1 {
			foreListWzhy := GetForeListByCode("wzhy", models.Db)

			var foreDetailWzhy models.ForeDetailJson
			if models.Db.Where("foreListId = ? AND state != ?", foreListWzhy.Id, "0").Order("time desc").First(&foreDetailWzhy).RowsAffected != 0 {
				var wzhyData = models.Forecast{}
				json.Unmarshal(foreDetailWzhy.Data, &wzhyData)
				for j, v := range wzhyData.Reports[1].Data {
					report.Data[j][3].Value = v[3].Value
				}
			}
			if len(raData.Reports) == 0 {
				return
			}
			waveValue := raData.Reports[0].Data[1][4].Value
			waveValueArr := tools.GetNumFloat64(waveValue)
			str := ""
			maxValue := tools.MaxFloat64(waveValueArr)
			if len(waveValueArr) == 2 {
				str = strings.Split(strings.Split(waveValue, "到")[0], "，")[1]
				report.Data[3][1].Value = tools.Float64ToFixedStr(waveValueArr[0]+0.1, 1) + "米，" + str + "到" + tools.Float64ToFixedStr(waveValueArr[1]+0.1, 1) + "米"
				if maxValue > 1.2 {
					report.Data[2][1].Value = tools.Float64ToFixedStr(waveValueArr[0]-0.1, 1) + "米，" + str + "到" + tools.Float64ToFixedStr(waveValueArr[1]-0.1, 1) + "米"
				} else {
					report.Data[2][1].Value = tools.Float64ToFixedStr(waveValueArr[0], 1) + "米，" + str + "到" + tools.Float64ToFixedStr(waveValueArr[1], 1) + "米"
				}
				report.Data[1][1].Value = tools.Float64ToFixedStr((waveValueArr[0]+0.1)/2-0.01, 1) + "米，" + str + "到" + tools.Float64ToFixedStr((waveValueArr[1]+0.1)/2-0.01, 1) + "米"
				report.Data[4][1].Value = tools.Float64ToFixedStr((waveValueArr[0]+0.1)/2-0.01, 1) + "米，" + str + "到" + tools.Float64ToFixedStr((waveValueArr[1]+0.1)/2-0.01, 1) + "米"
			} else if len(waveValueArr) == 1 {
				report.Data[3][1].Value = tools.Float64ToFixedStr(waveValueArr[0]+0.1, 1) + "米"
				if maxValue > 1.2 {
					report.Data[2][1].Value = tools.Float64ToFixedStr(waveValueArr[0]-0.1, 1) + "米"
				} else {
					report.Data[2][1].Value = tools.Float64ToFixedStr(waveValueArr[0], 1) + "米"
				}
				report.Data[1][1].Value = tools.Float64ToFixedStr((waveValueArr[0]+0.1)/2-0.01, 1) + "米"
				report.Data[4][1].Value = tools.Float64ToFixedStr((waveValueArr[0]+0.1)/2-0.01, 1) + "米"
			}

			for j := 1; j < 5; j++ {
				vValue := report.Data[j][1].Value
				vValueArr := tools.GetNumFloat64(vValue)
				if len(vValueArr) == 2 {
					//report.Data[j][2].Value
					if getWaveGrade(vValueArr[0]) == getWaveGrade(vValueArr[1]) {
						report.Data[j][2].Value = getWaveGrade(vValueArr[0])
					} else {
						report.Data[j][2].Value = getWaveGrade(vValueArr[0]) + "到" + getWaveGrade(vValueArr[1])
					}
				} else if len(vValueArr) == 1 {
					report.Data[j][2].Value = getWaveGrade(vValueArr[0])
				}
			}
		}
		//else {
		//	report.Data[1][1].Value = raData.Reports[0].Data[1][4].Value
		//	report.Data[1][2].Value = raData.Reports[0].Data[1][7].Value
		//	report.Data[2][1].Value = raData.Reports[0].Data[4][4].Value
		//	report.Data[2][2].Value = raData.Reports[0].Data[4][7].Value
		//}
	}
}
